﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using M7;
using System.IO;
using System.Threading;
using System.Data.Linq;
using System.Windows.Media.Imaging;
using S7.Windows.Menu;

namespace S7
{
    public partial class Booking
    {
        #region OldSearch
        public int? SearchByBookingID()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingSearchByBookingID(BookingID, ref iResult);
            return iResult;
        }

        public int? SearchByTime()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingSearchByBookingDate(BookingDate, ref iResult);
            return iResult;
        }

        public int? SearchByStatus()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingSearchByBookingStatus(BookingStatus, ref iResult);
            return iResult;
        }

        public int? SearchByTableID()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingSearchByTableID(TableID, ref iResult);
            return iResult;
        }
        public int? SearchByTableAndStatus()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingSearchByTableIDAndBookingStatus(TableID, BookingStatus, ref iResult);
            return iResult;
        }
        public void SearchByTableAndBookingTime() 
        {
            try
            {
                //int? iResult = null;
                BookingDate = DateTime.Today;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                //objDataAccessDataContext.BookingSearchByTableIDAndBookingTime(TableID, BookingDate, ref iResult);
                var checkReservation = from booking in objDataAccessDataContext.Bookings
                                       where booking.BookingDate == BookingDate && booking.TableID == TableID
                                       select new
                                       {
                                           booking.BookingTime
                                       };
            }
            catch (Exception)
            {
                //Mistrio: Unknown Exception Show ex.Message
                throw;
            }
            
        }

        public int? SearchByCustomerID()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingSearchByCustomerID(CustomerID, ref iResult);
            return iResult;
        }
        public int? SearchByCustomerAndStatus()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingSearchByCustomerIDAndBookingStatus(CustomerID, BookingStatus, ref iResult);
            return iResult;
        }
        public int? SearchByCustomer()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext
            return iResult;
        }
        public int? SearchByCustomerAndBookingTime()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.BookingSearchByCustomerIDAndBookingTime(CustomerID, BookingTime, ref iResult);
            return iResult;
        }

        public int? SearchByCustomerAndTable()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingSearchByCustomerIDAndTableID(CustomerID, TableID, ref iResult);
            return iResult;
        }
        #endregion

        public string Add()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.BookingAdd(CustomerID, TableID, MoneyCharged,
                    BookingDate, BookingTime, ref iResult);
                if ( iResult == -2 )
                {
                    errorMessage = "Already Booked";
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        public string Cancel()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingCancel(BookingID, ref iResult);
            return iResult.ToString();
        }

        public string Edit()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.BookingEditDateAndTime(BookingID, BookingDate, BookingTime, ref iResult);
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        public string Delete()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.BookingDelete(BookingID, ref iResult);
            return iResult.ToString();
        }

        #region Search

        public static List<Booking> Search()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            var query = from booking in objDataAccessDataContext.Bookings
                        select booking;
            return query.ToList();
        }

        public static List<Booking> Pending()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            try
            {
                var bookings = from booking in obj.Bookings
                               where booking.BookingStatus.Equals(0)
                               select booking;
                return bookings.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion
    }

    public partial class Customer: IEquatable<Customer>
    {
        public static List<Customer> lstCustomers;
        public static int? curCustomerID;

        public Customer(NCustomer cust)
        {
            this.CustomerID = cust.CustomerID;
            this.UserName = cust.Username;
            this.Password = cust.Password;
            this.FirstName = cust.FirstName;
            this.LastName = cust.LastName;
            this.Email = cust.Email;
            this.Address = cust.Address;
            this.Language = cust.Language;
            this.Mobile = cust.Mobile;
            this.Picture = cust.Picture;
        }
        public NCustomer ToNCustomer()
        {
            NCustomer cust = new NCustomer();
            cust.FirstName = FirstName;
            cust.LastName = LastName;
            cust.Mobile = Mobile;
            cust.Password = Password;
            cust.Email = Email;
            cust.CreditCardID = CreditCardID;
            cust.Address = Address;
            cust.Language = Language;
            cust.Picture = Picture;
            cust.Username = UserName;
            return cust;
        }

        public string SignIn()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.CustomerSignIn(UserName, Password, ref iResult);
                var query = from cust in objDataAccessDataContext.Customers
                            where cust.CustomerID == CustomerID
                            select new { cust.Picture };
                foreach (var cust in query)
                {
                    this.Picture = cust.Picture;
                }

                if (iResult == -1)
                {
                    errorMessage = "Wrong UserName or Password";
                }
                else
                {
                    CustomerID = (int)iResult;
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        public string SignOut()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.CustomerSignOut(CustomerID, ref iResult);
                if (iResult == -1)
                {
                    errorMessage = "User is already offline";
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        public string SignUp()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.CustomerSignUp(UserName, Password, FirstName, LastName, Address, Mobile, Email,
                    CreditCardID, Language, ref iResult);
                if (iResult == -2)
                {
                    errorMessage = "User Name already exists";
                }
                else
                {
                    CustomerID = (int)iResult;
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        public List<Customer> GetInfo()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            var customerInfo = from cust in objDataAccessDataContext.Customers
                               where cust.CustomerID == CustomerID
                               select cust;
            return customerInfo.ToList();
        }

        public string EditInfo()
        {
            int? iResult = null;
            string error = "";
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            try
            {
                objDataAccessDataContext.EditCustomerInfo(UserName, Password, FirstName, LastName, Address, Mobile, Email,
                        CreditCardID, Language, ref iResult);
                if (iResult.Equals(-1))
                {
                    error = "Customer doesn't exist";
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }

        #region Search

        public static List<Customer> OnlineCustomers()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();

            var query = from cust in objDataAccessDataContext.Customers
                        from online in objDataAccessDataContext.CustomerSignInOuts
                        where online.UserStatus.Equals(1) && cust.CustomerID.Equals(online.CustomerID)
                        select cust;
            return query.ToList();
        }

        public static List<Customer> Search()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            try
            {
                var customers = from cust in objDataAccessDataContext.Customers select cust;
                return customers.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static List<Customer> SearchText(string Text)
        {
            List<string> textwords = Info.ToStringsList(Text);
            List<Customer> lstResult = lstCustomers.ToList();
            foreach (var word in textwords)
            {
                var customers = from cust in lstCustomers
                                from wordsEmail in Info.ToStringsList(cust.Email)
                                from wordsLocation in Info.ToStringsList(cust.Address)
                                where cust.Email.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                || cust.UserName.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                || cust.FirstName.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                || cust.LastName.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                || wordsLocation.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                || wordsEmail.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                select cust;
                if (customers.ToList().Count == 0)
                {
                    lstResult.Clear();
                    break;
                }
                else
                {
                    lstResult = customers.Intersect(lstResult).ToList();
                }
            }

            return lstResult;
        }
        #endregion

        public string DeleteByManager()
        {
            string error = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.CustomerDeleteByManager(CustomerID, ref iResult);
                if (iResult == -1)
                {
                    error = "Customer doesn't exist";
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }
        public string Delete()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.CustomerDeleteByHimself(CustomerID, Password, ref iResult);
                if (iResult == -1)
                {
                    errorMessage = "Wrong Password";
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        #region IEquatable<Customer> Members

        public bool Equals(Customer other)
        {
            //Check whether the compared object is null.
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data.
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the products' properties are equal.
            return CustomerID.Equals(other.CustomerID);
        }

        #endregion

        #region Unused

       

        #endregion
    }

    public partial class Employee: IEquatable<Employee>
    {
        public static List<Employee> lstEmp;
        public static Employee currEmp;

        public int? EditInfo()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccess = new DataAccessDataContext();
            objDataAccess.EditEmployeesInfo(EmployeeID, Password, FirstName, LastName, JobName, Address,
                Mobile, Email, DepartmentID, ManagerID, Salary, Commission, Shift, ref iResult);
            return iResult;
        }

        #region Search
        public static List<Employee> Search()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            var query = from emp in obj.Employees
                        select emp;
            return query.ToList();
        }

        public static List<Employee> SearchText(string Text)
        {
            List<string> textwords = Info.ToStringsList(Text);
            List<Employee> lstResult = lstEmp.ToList();
            foreach (var word in textwords)
            {
                var employees = from emp in lstEmp
                                //from wordsEmail in Info.ToStringsList(emp.Email)
                                //from wordsFirstName in Info.ToStringsList(emp.FirstName)
                                //from wordsLastName in Info.ToStringsList(emp.LastName)
                                where emp.Email.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                || emp.FirstName.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                || emp.LastName.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                                select emp;
                if (employees.ToList().Count == 0)
                {
                    lstResult.Clear();
                    break;
                }
                else
                {
                    lstResult = lstResult.Intersect(employees.ToList()).ToList();
                }
            }

            return lstResult;
        } 
        #endregion

        public int? Add()
        {
            int? iResult = null;
            DataAccessDataContext obj = new DataAccessDataContext();
            //obj.EmployeeAdd(
            return iResult;
        }

        public int? Delete()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.EmployeeDeleteEmployeeByManager(EmployeeID, ref iResult);
            return iResult;
        }

        public int? SignIn()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.EmployeeSignIn(EmployeeID, Password, ref iResult);
            return iResult;
        }

        public int? SignOut()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.EmployeeSignOut(EmployeeID, ref iResult);
            return iResult;
        }

        #region IEquatable<Employee> Members

        public bool Equals(Employee other)
        {
            //Check whether the compared object is null.
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data.
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the products' properties are equal.
            return EmployeeID.Equals(other.EmployeeID);
        }

        #endregion

        #region Unused
        public int? SearchByID()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.EmployeeSearchInfoByEmployeeID(EmployeeID, ref iResult);
            return iResult;
        }
        public int? SearchByName(string strName)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.EmployeeSearchInfoByName(strName, ref iResult);
            return iResult;
        }
        #endregion
    }

    public partial class Favorite
    {
        
        public string Add()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.FavoriteAdd(CustomerID, MenuItemID, ref iResult);
                if (iResult == -2)
                {
                    errorMessage = "Item is already favorite";
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            
            return errorMessage;
        }

        #region Unused

        public int? DeleteByFavoriteID()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.FavoriteDeleteByFavoriteID(FavoriteID, ref iResult);
            return iResult;
        }
        public string DeleteByCustomerIDAndMenuItemID()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.FavoriteDeleteByCustomerIDAndMenuItemID(CustomerID, MenuItemID, ref iResult);
                if (iResult == -1)
                {
                    errorMessage = "Not found";
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }
        public int? DeleteByCustomerID()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.FavoriteDeleteByCustomerID(CustomerID, ref iResult);
            return iResult;
        }

        public int? Search(int? iCustomerID)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.FavoriteSearchByCustomerID(iCustomerID, ref iResult);
            var favorites = from favorite in objDataAccessDataContext.Favorites
                            where favorite.CustomerID == CustomerID
                            select favorite;
            return iResult;
        }
        public int? SearchByMenuItem(int? iMenuItem)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.FavoriteSearchByMenuItemID(iMenuItem, ref iResult);
            return iResult;
        }
        #endregion
    }

    public partial class Feedback
    {
        public Feedback(ref NFeedback nFeed)
        {
            //edi
        }

        public string Add()
        {
            string errorMessage = "";
            try
            {
                //int? iResult = null;
                //DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                //objDataAccessDataContext.FeedbackAddAll();
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }


        public static List<Feedback> Search()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            var query = from feed in obj.Feedbacks
                        select feed;
            return query.ToList();
        }

        public static List<Feedback> Unread()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            var q = from feed in objDataAccessDataContext.Feedbacks
                    where feed.Status == 0
                    select feed;
            return q.ToList();
        }

        #region Unused

        public int? SearchByCustomer(int? iCustomerID)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.FeedbackSearchByCustomer(iCustomerID, ref iResult);
            return iResult;
        }
        public int? SearchByRate(int? iFeedbackRate)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.FeedbackSearchByFeedbackRate(iFeedbackRate, ref iResult);
            return iResult;
        }

        #endregion
    }

    public partial class Menus
    {
        public static List<Menus> lstMenus;
        public NMenus ToNMenu()
        {
            var nMenu = new NMenus();
            nMenu.MenuID = this.MenuID;
            nMenu.MenuName = this.MenuName;
            return nMenu;
        }
        public string Add()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.MenusAdd(MenuName, ref iResult);
                if (iResult == -2)
                {
                    errorMessage = "A menu with this name already exists";
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        public int? Delete()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.MenusDelete(MenuID, ref iResult);
            return iResult;
        }

        public static List<Menus> Search()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            try
            {
                var menus = from menu in objDataAccessDataContext.Menus select menu;
                return menus.ToList();
            }
            catch (Exception)
            {
                return null;
            } 
        }
    }

    public partial class MenuItem: IEquatable<MenuItem>
    {
        public static List<MenuItem> lstItems;

        public static int? currID;

        #region Constructors
        public MenuItem(NMenuItem nMenuItem)
        {
            MenuID = nMenuItem.MenuID;
            MenuItemDescription = nMenuItem.Description;
            MenuItemID = nMenuItem.ID;
            MenuItemName = nMenuItem.Name;
            MenuItemPrice = nMenuItem.Price;
            Picture = nMenuItem.Picture;
            ThumbsUp = nMenuItem.ThumbsUp;
            ThumbsDown = nMenuItem.ThumbsDown;
        }
        public NMenuItem ToNMenuItem()
        {
            NMenuItem nMenuItem = new NMenuItem();
            nMenuItem.MenuID = this.MenuID;
            nMenuItem.Description = this.MenuItemDescription;
            nMenuItem.ID = this.MenuItemID;
            nMenuItem.Name = this.MenuItemName;
            nMenuItem.Price = this.MenuItemPrice;
            nMenuItem.Picture = this.Picture;
            nMenuItem.ThumbsUp = this.ThumbsUp;
            nMenuItem.ThumbsDown = this.ThumbsDown;
            return nMenuItem;
        }
        #endregion

        public string Add()
        {
            string errorMessage = "";
            try
            {
                int? iResult = null;
                DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
                objDataAccessDataContext.MenuItemsAdd(MenuItemName, MenuItemDescription, MenuItemPrice, MenuID, Picture,
                    ref iResult);
                if (iResult == -2)
                {
                    errorMessage = "Menu Item with this name already exists";
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        public int? Delete()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.MenuItemsDelete(MenuItemID, ref iResult);
            return iResult;
        }

        public BitmapImage ArrayToImage(byte[] data)
        {
            MemoryStream strm = new MemoryStream();

            strm.Write(data, 0, data.Length);

            strm.Position = 0;

            System.Drawing.Image img = System.Drawing.Image.FromStream(strm);

            BitmapImage bi = new BitmapImage();

            bi.BeginInit();

            MemoryStream ms = new MemoryStream();

            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

            ms.Seek(0, SeekOrigin.Begin);

            bi.StreamSource = ms;

            bi.EndInit();

            return bi;
        }

        #region Unused
        public int? Edit() //Old Method
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.EditMenuItem(int.Parse(arrMenuItem[0].ToString()),arrMenuItem[1].ToString(),
            //arrMenuItem[2].ToString(),decimal.Parse(arrMenuItem[3].ToString()), int.Parse(arrMenuItem[4].ToString()),
            //(System.Data.Linq.Binary)arrMenuItem[5], int.Parse(arrMenuItem[6].ToString()),(DateTime?)arrMenuItem[7], iResult);
            return iResult;
        }
        public int? Edit(int? iMenuItemID, string strImageURL)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.MenuItemsEditPicture(iMenuItemID, strImageURL, ref iResult);
            return iResult;
        }
        public int? Edit(int? iMenuItemID, string strItemName, string strDescritpion)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.MenuItemsEditItemNameAndDescription(iMenuItemID, strItemName, strDescritpion, ref iResult);
            return iResult;
        }
        public int? Edit(int? iMenuItemID, int? iMenuID)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.MenuItemsEditMenuID(iMenuItemID, iMenuID, ref iResult);
            return iResult;
        }
        public int? Edit(int? iMenuItemID, decimal? dPrice, int? iScore)
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            //objDataAccessDataContext.MenuItemsEditPriceAndScore(iMenuItemID, dPrice, iScore, ref iResult);
            return iResult;
        }
        #endregion

        #region Search
        public static List<MenuItem> Search()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            var menuItems = from menuItem in objDataAccessDataContext.MenuItems
                            select menuItem;
            return menuItems.ToList();
        }

        public static List<MenuItem> TextSearch(string Text)
        {
            List<string> textwords = Info.ToStringsList(Text);
            List<MenuItem> lstResult = MenuItem.lstItems.ToList();
           // Dictionary<string, AutoResetEvent> thrEvents = new Dictionary<string, AutoResetEvent>();
            foreach (var word in textwords)
            {
                List<MenuItem> menuItems = SearchAWord(word);
                if (menuItems.ToList().Count == 0)
                {
                    lstResult.Clear();
                    break;
                }
                else
                {
                    lstResult = lstResult.Intersect(menuItems).ToList();
                }
            }
            return lstResult.Distinct().ToList();
        }

        private static List<MenuItem> SearchAWord(string Word)
        {
            var menuItems = from item in MenuItem.lstItems
                            from NameWord in Info.ToStringsList(item.MenuItemName)
                            from DescWord in Info.ToStringsList(item.MenuItemDescription)
                            where DescWord.ToLowerInvariant().StartsWith(Word.ToLowerInvariant()) 
                            || NameWord.ToLowerInvariant().StartsWith(Word.ToLowerInvariant())
                            select item;
            return menuItems.ToList();
        } 
        #endregion

        #region IEquatable<MenuItem> Members

        public bool Equals(MenuItem other)
        {
            //Check whether the compared object is null.
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data.
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the products' properties are equal.
            return MenuItemID.Equals(other.MenuItemID);
        }

        #endregion
    }

    public partial class Order
    {
        public static List<Order> lstOrders;
        public Order(NOrder nOrder)
        {
            this.OrderID = nOrder.OrderID;
            this.OrderDate = nOrder.OrderDate;
            //this.OrderTime = nOrder.OrderTime;
            this.TableID = nOrder.TableID;
            //this.EmployeeID = nOrder.EmployeeID;
            this.CustomerID = nOrder.CustomerID;
        }
        
        public string Add()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.OrderAdd(CustomerID,TableID, ref iResult);
            return iResult.ToString();
        }

        #region Search

        public static List<Order> Search()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            try
            {
                var orders = from order in obj.Orders
                             select order;
                return orders.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static List<Order> TextSearch(string Text)
        {
            //List<string> textwords = Info.ToStringsList(Text);
            //List<Order> lstResult = lstOrders.ToList();
            //foreach (var word in textwords)
            //{
            //    var orders = from order in lstOrders
            //                    from wordsEmail in Info.ToStringsList(order.)
            //                    from wordsLocation in Info.ToStringsList(cust.Address)
            //                    where cust.Email.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
            //                    select order;
            //    if (orders.ToList().Count == 0)
            //    {
            //        lstResult.Clear();
            //        break;
            //    }
            //    else
            //    {
            //        lstResult = orders.Intersect(lstResult).ToList();
            //    }
            //}

            //return lstResult;
            return null;
        }

        public static List<Order> Pending()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            try
            {
                var orders = from order in obj.Orders
                             where order.OrderStatus == 0
                             select order;
                return orders.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<MenuItem> NotPaid()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            try
            {
                var orders = from order in obj.Orders
                             where order.OrderStatus == 0
                             && order.CustomerID.Equals(CustomerID)
                             select order;

                List<int> ItemsID = new List<int>();
                foreach (var order in orders)
                {
                    List<OrderMenuItem> orderItemsList = order.OrderMenuItems.ToList();
                    foreach (var orderMenuItem in orderItemsList)
                    {
                        for (int i = 0; i < orderMenuItem.MenuItemQuantity; i++)
                        {
                            ItemsID.Add(orderMenuItem.MenuItemID);
                        }
                    }
                }
                List<MenuItem> lstItems = MenuItem.Search();
                List<MenuItem> ResultItems = new List<MenuItem>();
                foreach (var ID in ItemsID)
                {
                    var items = from item in lstItems
                                where item.MenuItemID.Equals(ID)
                                select item;
                    ResultItems.AddRange(items.ToList());
                }
                return ResultItems;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion
    }

    public partial class OrderMenuItem
    {
        public OrderMenuItem(NOrderMenuItem nItem, int orderID)
        {
            this.AtDate = nItem.AtDate;
            this.MenuItemID = nItem.MenuItemID;
            this.MenuItemQuantity = nItem.MenuItemQuantity;
            this.OrderID = orderID;
            this.MenuItemComment = nItem.MenuItemComment;
        }
        public void Add()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.OrderMenuItemAdd(MenuItemID, MenuItemQuantity, MenuItemComment, OrderID);
        }
    }

    public partial class Table: IEquatable<Table>
    {
        public static List<Table> lstTables;

        public int? Add()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            if (IP == null)
            {
                IP = "0";
            }
            if (Port == null)
            {
                Port = 0;
            }
            objDataAccessDataContext.TableAdd(TableID, NumberOfSeats, TableDetails,IP, Port, ref iResult);
            return iResult;
        }

        public int? Delete()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.TableDelete(TableID, ref iResult);
            return iResult;
        }

        public int? Edit()
        {
            int? iResult = null;
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            objDataAccessDataContext.TableEdit(TableID, NumberOfSeats, TableDetails,IP ,Port, ref iResult);
            return iResult;
        }

        #region Search
        public static List<Table> Search()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            var tables = from table in objDataAccessDataContext.Tables
                         select table;
            return tables.ToList();
        }
        #endregion

        #region IEquatable<Table> Members

        public bool Equals(Table other)
        {
            //Check whether the compared object is null.
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data.
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the products' properties are equal.
            return TableID.Equals(other.TableID) || IP.Equals(other.IP);
        }

        #endregion
    }

    public partial class TV
    {
        public static List<TV> lstTV;

        public static List<TV> Search()
        {
            DataAccessDataContext objDataAccessDataContext = new DataAccessDataContext();
            try
            {
                var channels = from channel in objDataAccessDataContext.TVs select channel;
                return channels.ToList();
            }
            catch (Exception)
            {
                return null;
            } 
        }
    }

    public partial class Music
    {
        public static void LoadMusic(string Path)
        {
            DirectoryInfo di = new DirectoryInfo(Path);

            List<DirectoryInfo> lstGenres = di.GetDirectories().ToList();

            foreach (var genreDir in lstGenres)
            {
                Genre genre = new Genre();
                genre.GenreName = genreDir.Name;
                int? GenreID = genre.Add();
                if (GenreID >= 0)
                {
                    foreach (var artistDir in genreDir.GetDirectories())
                    {
                        Artist artist = new Artist();
                        artist.ArtistName = artistDir.Name;
                        int? ArtistID = artist.Add();
                        if (ArtistID >= 0)
                        {
                            foreach (var file in artistDir.GetFiles("*.mp3"))
                            {
                                Song song = new Song();
                                song.SongName = ReturnSongName(file.Name);
                                song.GenreID = GenreID;
                                song.ArtistID = ArtistID;
                                song.SongPath = file.FullName;
                                string lyricsPath = artistDir.FullName + "\\" + song.SongName + ".txt";
                                string lyrics = null;
                                if (File.Exists(lyricsPath))
                                {
                                    StreamReader sr = new StreamReader(lyricsPath);
                                    lyrics = sr.ReadToEnd();
                                    sr.Dispose();
                                    sr.Close();
                                }
                                song.Lyrics = lyrics;
                                song.Add();
                            }
                        }
                    }
                }
            }
        }

        private static string ReturnSongName(string path)
        {
            var query = from c in path
                        select c;
            StringBuilder sbSongName = new StringBuilder();
            List<char> name = query.ToList();
            for (int i = 0; i <= name.Count; i++)
            {
                if (name[i] != '.')
                {
                    sbSongName.Append(name[i]);
                }
                else
                {
                    break;
                }
            }
            return sbSongName.ToString();
        }

        public static List<Song> SearchText(string Text)
        {
            List<string> textwords = Info.ToStringsList(Text);
            List<Song> lstResult = Song.lstSongs.ToList();
            foreach (var word in textwords)
            {
                var songs = from song in Song.lstSongs
                            from artist in Artist.lstArtists
                            from songWords in Info.ToStringsList(song.SongName)
                            from artistWords in Info.ToStringsList(artist.ArtistName)
                            where songWords.ToLowerInvariant().StartsWith(word.ToLowerInvariant())
                            || artistWords.ToLowerInvariant().StartsWith(word)
                            && song.ArtistID == artist.ArtistID
                            select song;

                if (songs.ToList().Count == 0)
                {
                    lstResult.Clear();
                    break;
                }
                else
                {
                    lstResult = songs.Intersect(lstResult).ToList();
                }
            }
            return lstResult;
        }
    }

    public partial class AC
    {
        private int _ID;
        private string _Temp;


        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public string Temp
        {
            get { return _Temp; }
            set { _Temp = value; }
        }
    }

    public partial class Song
    {
        public static List<Song> lstSongs;
       
        public static List<Song> Search()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            var songs = from song in obj.Songs
                        select song;
            return songs.ToList();
        }

        public int? Add()
        {
            int? iResult = null;
            DataAccessDataContext obj = new DataAccessDataContext();
            obj.SongAdd(SongName, SongPath, ArtistID, GenreID, Lyrics, ref iResult);
            return iResult;
        }

        public int? Delete()
        {
            int? iResult = null;
            DataAccessDataContext obj = new DataAccessDataContext();
            obj.SongDelete(SongID, ref iResult);
            return iResult;
        }
    }

    public partial class Genre: IEquatable<Genre>
    {
        public static List<Genre> lstGenre;
        public int? Add()
        {
            int? iResult = null;
            DataAccessDataContext obj = new DataAccessDataContext();
            obj.GenreAdd(GenreName, ref iResult);
            return iResult;
        }

        public static List<Genre> Search()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            var genres = from genre in obj.Genres
                         select genre;
            return genres.ToList();
        }

        #region IEquatable<Genre> Members

        public bool Equals(Genre other)
        {
            //Check whether the compared object is null.
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data.
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the products' properties are equal.
            return GenreID.Equals(other.GenreID);
        }

        #endregion
    }

    public partial class Artist
    {
        public static List<Artist> lstArtists;

        public static List<Artist> Search()
        {
            DataAccessDataContext obj = new DataAccessDataContext();
            var artists = from artist in obj.Artists
                          select artist;
            return artists.ToList();
        }

        public int? Add()
        {
            int? iResult = null;
            DataAccessDataContext obj = new DataAccessDataContext();
            obj.ArtistAdd(ArtistName, ref iResult);
            return iResult;
        }
    }

    public partial class AdminTools
    {
        public static Dictionary<enumModules, bool> GetModules()
        {
            Dictionary<enumModules, bool> moduleOptions = new Dictionary<enumModules, bool>();
            moduleOptions.Add(enumModules.Chat, Properties.Settings.Default.ChatModule);
            moduleOptions.Add(enumModules.CheckOut, Properties.Settings.Default.CheckOutModule);
            moduleOptions.Add(enumModules.Games, Properties.Settings.Default.GamesModule);
            moduleOptions.Add(enumModules.Internet, Properties.Settings.Default.InternetModule);
            moduleOptions.Add(enumModules.Memoire, Properties.Settings.Default.MemoireModule);
            moduleOptions.Add(enumModules.Personalization, Properties.Settings.Default.PersonalizationModule);
            moduleOptions.Add(enumModules.Menu, Properties.Settings.Default.MenuModule);
            return moduleOptions;
        }
    }

    
}
